using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.Smo;
using System;
using System.Collections.Generic;

namespace Microsoft.SqlServer.Management.SqlManagerUI.Scripting
{
using Server = Microsoft.SqlServer.Management.Smo.Server;
	internal class TriggerDiscovery
	{
		public IEnumerable<Urn> GetTriggers(Server server, IEnumerable<Urn> urns)
		{
			if (server == null)
			{
				throw new ArgumentNullException("server");
			}
			if (urns == null)
			{
				throw new ArgumentNullException("urns");
			}
			List<Urn> list = new List<Urn>();
			foreach (Urn current in urns)
			{
				if (current.Type == "Trigger")
				{
					list.Add(current);
				}
				else if (current.Type == "Table" || current.Type == "View")
				{
					list.AddRange(this.GetTriggersForTable(server, current));
				}
				else if (current.Type == "View")
				{
					list.AddRange(this.GetTriggersForView(server, current));
				}
			}
			return list;
		}

		protected virtual IEnumerable<Urn> GetTriggersForTable(Server server, Urn tableUrn)
		{
			List<Urn> list = new List<Urn>();
			TableViewBase tableViewBase = (TableViewBase)server.GetSmoObject(tableUrn);
			foreach (Trigger trigger in tableViewBase.Triggers)
			{
				if (!trigger.IsSystemObject)
				{
					list.Add(trigger.Urn);
				}
			}
			return list;
		}

		protected virtual IEnumerable<Urn> GetTriggersForView(Server server, Urn viewUrn)
		{
			List<Urn> list = new List<Urn>();
			View view = (View)server.GetSmoObject(viewUrn);
			foreach (Trigger trigger in view.Triggers)
			{
				if (!trigger.IsSystemObject)
				{
					list.Add(trigger.Urn);
				}
			}
			return list;
		}
	}
}
